﻿package mbalib.docviewer
{
	import com.adobe.serialization.json.JSON;
	
	import flash.display.*;
	import flash.events.*;
	import flash.external.ExternalInterface;
	import flash.geom.Point;
	import flash.net.*;
	import flash.text.*;
	import flash.ui.Keyboard;
	import flash.utils.*;
	
	import mbalib.ui.events.*;
	import mbalib.ui.managers.*;

	public class DocViewer extends Sprite
	{
		private var _container:Sprite;
		private var _curZoom:Number = 1;
		private const X_PADDING:Number = 7;
		private const Y_PADDING:Number = 10;
		private var _pagesLoaded:uint = 0;
		private var _pageHeight:Number;
		private var _docURL:String;
		private var _hasConvertPages:uint;
		private var _pagesLoadedArray:Array;
		private var _pageth:uint = 0;
		private var _mouseDown:Boolean = false;
		private const DV_HEIGHT:Number = 665;
		private var _snapText:TextSnapshot;
		private var _pagethLoading:int;
		private var _scrollPane:ScrollPane;
		private var _snapText2:TextSnapshot;
		private var _snapText0:TextSnapshot;
		private var _snapText1:TextSnapshot;
		private var _normalPageNum:int;
		private var _pagesAll:uint = 0;
		private var _convertLoader:Loader;
		private var _resultNum:int;
		private var _byteArray:Array;
		private var _convertErrorIntervalID:int;
		private var _noDoc:TextField;
		private const DV_WIDTH:Number = 780;
		private var _fromPage:Number;
		private var _loadPercent:Number;
		private const DV_PAGE_WIDTH:Number = 681;
		private var _showLoading:Boolean = true;
		private var _toPage:Number;
		private var _inSwitching:Boolean = false;
		private var _displayState:String = "normal";
		private var _positionY:Number = 0;
		private var _allConvertPages:uint;
		private var _positionX:Number = 0;
		private const HIGHLIGHT_COLOR:int = 16776960;
		private var _inLoading:Boolean;
		private var _containerHeight:Number;
		private var _containerWidth:Number;
		private var _delayPreLoadID:int;
		private var _searchPageth:int = -1;
		private var _firstPagesNum:int;
		private var _pageWidth:Number;
		
		public function DocViewer()
		{
			return;
		}// end function
		
		public function get size() : Object
		{
			var _loc_1:* = Math.min(this._scrollPane.content.width, stage.stageWidth);
			return {contentWidth:_loc_1};
		}// end function
		
		public function get pagesLoadedArray() : Array
		{
			return this._pagesLoadedArray;
		}// end function
		
		public function get npn() : int
		{
			return this._normalPageNum;
		}// end function
		
		private function init() : void
		{
			var tmpURL:String;
			this._displayState = "normal";
			this._searchPageth = -1;
			if (this._scrollPane)
			{
				if (this._scrollPane.parent)
				{
					this._scrollPane.parent.removeChild(this._scrollPane);
				}// end if
				this._scrollPane = null;
			}// end if
			this._scrollPane = new ScrollPane();
			this._scrollPane.setSize(stage.stageWidth--, stage.stageHeight - 43);
			addChild(this._scrollPane);
			this._scrollPane.vScrollPolicy = "on";
			this._scrollPane.vScrollBar.addEventListener(ScrollEvent.SCROLL, this.docScroll);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, this.docKeyDown);
			stage.addEventListener(MouseEvent.MOUSE_DOWN, this.stageMouseEvent);
			stage.addEventListener(MouseEvent.MOUSE_UP, this.stageMouseEvent);
			stage.addEventListener(MouseEvent.MOUSE_WHEEL, this.doStageMouseWheelInFull_1026_full_same_normal);
			 
			if (this._container)
			{
				if (this._container.parent)
				{
					this._container.parent.removeChild(this._container);
				}// end if
				this._container = null;
			}// end if
			this._container = new Sprite();
			var binaryLoader:* = new URLLoader();
			binaryLoader.dataFormat = "binary";
			binaryLoader.addEventListener(ProgressEvent.PROGRESS, this.binaryLoading);
			binaryLoader.addEventListener(Event.COMPLETE, this.binaryLoadComplete);
			binaryLoader.addEventListener(IOErrorEvent.IO_ERROR, this.binaryLoadError);
			if (this._firstPagesNum == -1)
			{
				tmpURL = this._docURL + "pn=" + (this._pagesLoaded + 1) + "&rn=" + this._normalPageNum;
			}
			else
			{
				tmpURL = this._docURL + "pn=1&rn=" + this._firstPagesNum;
				this._firstPagesNum = -1;
			}// end else if
			trace(tmpURL);
			var binaryRequest:* = new URLRequest(tmpURL);
			try
			{
				binaryLoader.load(binaryRequest);
				if (this._showLoading)
				{
					dispatchEvent(new Event("SHOW_LOADING", true));
				}// end if
				this._inLoading = true;
			}// end try
			catch (err)
			{
				_inLoading = false;
				trace("load binary data error in:" + _docURL);
			}// end catch
			return;
		}// end function
		
		public function doSearch(param1:int, param2:String) : void
		{
			var id:int;
			var pageIndex:* = param1;
			var kywd:* = param2;
			this.closeSearchResult();
			id = setInterval(
				function ()
				{
					clearInterval(id);
					doSearchDELAY(pageIndex, kywd);
					return;
				}// end function
				, 50);
			return;
		}// end function
		
		public function closeSearchResult() : void
		{
			this._searchPageth = -1;
			if (this._snapText0)
			{
				this._snapText0 = null;
			}// end if
			if (this._snapText1)
			{
				this._snapText1 = null;
			}// end if
			if (this._snapText2)
			{
				this._snapText2 = null;
			}// end if
			return;
		}// end function
		
		public function set fpn(param1:int) : void
		{
			this._firstPagesNum = param1;
			return;
		}// end function
		
		private function docScroll(param1:ScrollEvent = null) : void
		{
			var _loc_2:* = param1 ? (param1.delta) : (0);
			if (this._pagesLoaded < this._pagesAll)
			{
				if (this._delayPreLoadID)
				{
					clearInterval(this._delayPreLoadID);
				}// end if
				this._delayPreLoadID = setInterval(this.preLoad, 1000, _loc_2);
			}// end if
			var _loc_3:* = Math.ceil(this._pagesAll * (this._scrollPane.vScrollPosition + stage.stageHeight / 2) / (this._scrollPane.maxVScrollPosition + this._scrollPane.vScrollBar.height)) - 1;
			if (this._pageth != _loc_3)
			{
				this._pageth = _loc_3;
				dispatchEvent(new Event("PAGETH_CHANGED", true));
			}
			return;
		}// end function
		
		private function addBlankPagesX() : void
		{
			var _loc_1:BlankPage;
			var _loc_2:* = this._pagesLoaded;
			while (_loc_2++ < this._pagesAll)
			{
				// label
				this._containerWidth = this._container.width;
				this._containerHeight = this._container.height;
				_loc_1 = new BlankPage();
				_loc_1.pageth = _loc_2 + 1;
				_loc_1.setSize(this.DV_PAGE_WIDTH, this._pageHeight);
				this._container.addChild(_loc_1);
				_loc_1.x = 0;
				_loc_1.y = this._containerHeight + this.Y_PADDING;
			}// end while
			return;
		}// end function
		
		public function get resultNum() : int
		{
			return this._resultNum;
		}// end function
		
		public function set docURL(param1:String) : void
		{
			if (param1 != this._docURL)
			{
				this._docURL = param1;
				this.init();
			}// end if
			return;
		}// end function
		
		public function doSearchDELAY(param1:int, param2:String) : void
		{
			var _loc_6:Sprite;
			var _loc_7:int;
			var _loc_8:int;
			var _loc_9:String;
			var _loc_10:int;
			if (!param1 || param2 == "" || param1 == this._searchPageth)
			{
				return;
			}// end if
			var _loc_3:Array;
			_loc_3 = param2.split(" ");
			var _loc_4:Number;
			this._searchPageth = param1;
			var _loc_5:int;
			while (_loc_5 < 3)
			{
				// label
				if (_loc_5 + param1 > this._pagesAll--)
				{
					break;
				}// end if
				_loc_6 = this._container.getChildAt(_loc_5 + param1) as Sprite;
				this["_snapText" + _loc_5] = _loc_6.textSnapshot;
				_loc_7 = 0;
				_loc_8 = _loc_3.length;
				while (_loc_7 < _loc_8)
				{
					// label
					_loc_9 = _loc_3[_loc_7];
					if (_loc_9 != "")
					{
						_loc_10 = this["_snapText" + _loc_5].findText(0, _loc_9, false);
						if (_loc_10 == -1)
						{
						}
						else if (_loc_10 > 0)
						{
							do
							{
								// label
								this["_snapText" + _loc_5].setSelectColor(this.HIGHLIGHT_COLOR);
								this["_snapText" + _loc_5].setSelected(_loc_10, _loc_10 + _loc_9.length, true);
								_loc_10 = this["_snapText" + _loc_5].findText(_loc_10 + _loc_9.length, _loc_9, false);
							}while (_loc_10 > 0)
						}// end if
					}// end else if
					_loc_7++;
				}// end while
				_loc_5++;
			}// end while
			this._resultNum = _loc_4++;
			dispatchEvent(new Event("TO_SEARCH_RESULT", true));
			return;
		}// end function
		
		public function get pagesAll() : uint
		{
			return this._pagesAll;
		}// end function
		
		private function preLoad(param1:Number) : void
		{
			var bn:int;
			var yn:int;
			var pn:int;
			var tn:int;
			var binaryLoader:URLLoader;
			var tmpURL:String;
			var binaryRequest:URLRequest;
			var pnReady:Boolean;
			var tnReady:Boolean;
			var i:int;
			var delta:* = param1;
			clearInterval(this._delayPreLoadID);
			if (this._mouseDown)
			{
				this._delayPreLoadID = setInterval(this.preLoad, 1000, delta);
				return;
			}// end if
			var num:* = this._pageth;
			bn = Math.floor(num / 5);
			yn = num % 5;
			pn = bn * 5;
			if (yn == 2)
			{
				tn = pn + 4;
			}
			else if (yn < 2)
			{
				bn = bn--;
				pn = bn * 5;
				tn = pn + 9;
			}
			else
			{
				tn = pn + 9;
			}// end else if
			pn = Math.max(0, pn);
			tn = Math.min(tn, this._pagesAll--);
			trace("理论值：", pn, tn);
			trace(this._pagesLoadedArray);
			var iLen:* = tn - pn + 1;
			while (i < iLen)
			{
				
				if (!pnReady && !this._pagesLoadedArray[pn + i])
				{
					pnReady=true;
					pn = pn + i;
				}
				if (!tnReady && !this._pagesLoadedArray[tn - i])
				{
					tnReady=true;
					tn = tn - i;
				}
				if (pnReady && tnReady)
				{
					break;
				}
				i = (i + 1);
			}
			
			trace("调整之后:", pn, tn);
			trace("pnReady:" + pnReady, "tnReady:" + tnReady);
			 
			if (pnReady && pnReady && pn <= tn && pn < this._pagesAll)
			{
				trace("从" + (pn + 1), " 到" + (tn + 1));
			 
				binaryLoader = new URLLoader();
				binaryLoader.dataFormat = "binary";
				binaryLoader.addEventListener(ProgressEvent.PROGRESS, this.binaryLoading);
				binaryLoader.addEventListener(Event.COMPLETE, this.binaryLoadComplete);
				binaryLoader.addEventListener(IOErrorEvent.IO_ERROR, this.binaryLoadError);
				tmpURL = this._docURL + "pn=" + (pn + 1) + "&rn=" + (tn - pn + 1);
				trace(tmpURL);
				binaryRequest = new URLRequest(tmpURL);
				try
				{
					binaryLoader.load(binaryRequest);
					this._inLoading = true;
				}// end try
				catch (err)
				{
					_inLoading = false;
					trace("load binary data error in:" + _docURL);
				}// end if
			}// end catch
			return;
		}// end function
		
		public function get pageth() : uint
		{
			return this._pageth;
		}// end function
		
		private function onConvertErrorX(param1:Event = null) : void
		{
			trace("onConvertError");
			this._loadPercent = 0;
			var _loc_2:Object;
			_loc_2._hasConvertPages = this._hasConvertPages++;
			if (this._hasConvertPages == this._allConvertPages)
			{
				if (!this._pageth)
				{
					this._pageth = 0;
				}// end if
				dispatchEvent(new Event("STOP_LOADING", true));
				dispatchEvent(new Event("PAGE_LOADED", true));
				this._hasConvertPages = 0;
				this._allConvertPages = 0;
				this._convertLoader = null;
				this._byteArray = [];
				this._inLoading = false;
				this._showLoading = false;
				this.add2ScrollPane();
				this._containerWidth = this._container.width;
				this._containerHeight = this._container.height;
			}
			else
			{
				this.byteArr2DisplayObj(this._hasConvertPages);
			}// end else if
			return;
		}// end function
		
		private function doMouseWheel_1026_full_same_normal(param1:Number) : void
		{
			var toPageth:int;
			var upordown:int;
			var delta:* = param1;
			try
			{
				if (this._displayState == "normal")
				{
					delta = delta * 10;
					this._scrollPane.vScrollPosition = this._scrollPane.vScrollPosition + delta;
				}
				else
				{
					toPageth = this._pageth;
					upordown = delta < 0 ? (1) : (-1);
					if (this._displayState == "fullSingle")
					{
						toPageth = toPageth + 1 * upordown;
					}
					else if (this._displayState == "fullDouble")
					{
						toPageth = toPageth + 2 * upordown;
					}// end else if
					if (toPageth != this._pageth)
					{
						this.pageth = Math.max(0, Math.min(toPageth, this._pagesAll--));
						dispatchEvent(new Event("PAGETH_CHANGED", true));
					}// end if
				}// end else if
			}// end try
			catch (err)
			{
				trace("no scrollpane");
			}// end catch
			return;
		}// end function
		
		private function docKeyDown(param1:KeyboardEvent = null) : void
		{
			var _loc_2:Number;
			switch(param1.keyCode)
			{
				case Keyboard.UP:
				{
					_loc_2 = (-this._pageHeight) * 0.3;
					break;
				}// end case
				case Keyboard.DOWN:
				{
					_loc_2 = this._pageHeight * 0.3;
					break;
				}// end case
				case Keyboard.PAGE_UP:
				{
					_loc_2 = -this._pageHeight;
					break;
				}// end case
				case Keyboard.PAGE_DOWN:
				{
					_loc_2 = this._pageHeight;
					break;
				}// end case
				default:
				{
					break;
					break;
				}// end default
			}// end switch
			this._scrollPane.vScrollPosition = this._scrollPane.vScrollPosition + _loc_2;
			return;
		}// end function
		
		public function switchDisplayState(param1:String) : void
		{
			var cm:CursorManager;
			var cursorID:int;
			var status:* = param1;
			var stgWidth:* = stage.stageWidth;
			var stgHeight:* = stage.stageHeight;
			this.zoom = 1;
			if (status == "TO_FULL")
			{
				this._displayState = "fullSingle";
				stage.addEventListener(MouseEvent.MOUSE_WHEEL, this.doStageMouseWheelInFull);
				this._inSwitching = true;
				cm = CursorManager.getInstance();
				if (!cm.root)
				{
					cm.root = stage;
				}// end if
				cm.hideCurrentCursor();
				this._container.removeEventListener(MouseEvent.ROLL_OVER, this.docMouseOver);
				cursorID = setInterval(
					function ()
					{
						clearInterval(cursorID);
						_container.addEventListener(MouseEvent.ROLL_OVER, docMouseOver);
						if (_container.hitTestPoint(mouseX, mouseY))
						{
							cm.showCursor("ReadCursor");
						}
						else
						{
							cm.hideCurrentCursor();
						}// end else if
						_inSwitching = false;
						return;
					}// end function
					, 4000);
			}
			else
			{
				if (this._displayState == "fullDouble")
				{
					this.switchPagesMode("normal");
				}// end if
				this._displayState = "normal";
				stage.removeEventListener(MouseEvent.MOUSE_WHEEL, this.doStageMouseWheelInFull);
			}// end else if
			this._scrollPane.setSize(stgWidth--, stgHeight - 43);
			this._scrollPane.content.x = Math.max((stgWidth - this._container.width) / 2, 0);
			this._scrollPane.hScrollPosition = this._scrollPane.maxHScrollPosition / 2;
			this._scrollPane.update();
			var p:* = this._container.getChildAt(this._pageth) as Sprite;
			this._scrollPane.vScrollPosition = p.y * this.zoom;
			this.docScroll();
			return;
		}// end function
		
		private function addBlankPages() : void
		{
			var startIndex:int;
			var blankID:int;
			var blank:* = 
				function () : void
				{
					var _loc_1:BlankPage;
					var _loc_2:* = startIndex;
					var _loc_3:* = Math.min(_pagesAll, startIndex + 10);
					while (_loc_2++ < _loc_3)
					{
						// label
						_loadPercent = _loc_2 / _pagesAll;
						if (_showLoading)
						{
							dispatchEvent(new Event("SHOW_LOADING", true));
						}// end if
						_containerWidth = _container.width;
						_containerHeight = _container.height;
						_loc_1 = new BlankPage();
						_loc_1.pageth = _loc_2 + 1;
						_loc_1.setSize(DV_PAGE_WIDTH, _pageHeight);
						_container.addChild(_loc_1);
						_loc_1.x = 0;
						_loc_1.y = _containerHeight + Y_PADDING;
						startIndex++;
					}// end while
					if (startIndex == _pagesAll)
					{
						clearInterval(blankID);
						dispatchEvent(new Event("STOP_LOADING", true));
						dispatchEvent(new Event("PAGE_LOADED", true));
						_hasConvertPages = 0;
						_allConvertPages = 0;
						_convertLoader = null;
						_byteArray = [];
						_inLoading = false;
						_showLoading = false;
						add2ScrollPane();
						_containerWidth = _container.width;
						_containerHeight = _container.height;
					}// end if
					return;
				}// end function
				;
			startIndex = this._pagesLoaded;
			blankID = setInterval(blank, 50);
			return;
		}// end function
		
		public function doSearchXX(param1:int, param2:String) : void
		{
			var _loc_9:String;
			var _loc_10:int;
			var _loc_3:String;
			var _loc_4:int;
			if (!param2 || param2 == "" || param1 == this._searchPageth)
			{
				return;
			}// end if
			this._searchPageth = param1;
			var _loc_5:* = this._container.getChildAt(param1) as Sprite;
			this._scrollPane.vScrollPosition = _loc_5.y;
			this._snapText = _loc_5.textSnapshot;
			this._snapText.setSelected(0, this._snapText.charCount, false);
			var _loc_6:* = param2.split(" ");
			var _loc_7:Number;
			var _loc_8:int;
			while (_loc_8 < _loc_6.length)
			{
				// label
				_loc_9 = _loc_6[_loc_8];
				if (_loc_9 != "")
				{
					_loc_10 = this._snapText.findText(0, _loc_9, false);
					if (_loc_10 >= 0)
					{
						do
						{
							// label
							this._snapText.setSelectColor(this.HIGHLIGHT_COLOR);
							this._snapText.setSelected(_loc_10, _loc_10 + _loc_9.length, true);
							_loc_10 = this._snapText.findText(_loc_10 + _loc_9.length, _loc_9, false);
						}while (_loc_10 >= 0)
					}// end if
				}
				else
				{
					this._snapText.setSelected(0, this._snapText.charCount, false);
				}// end else if
				_loc_8++;
			}// end while
			this._resultNum = _loc_7++;
			dispatchEvent(new Event("TO_SEARCH_RESULT", true));
			return;
		}// end function
		
		private function preLoadX() : void
		{
			clearInterval(this._delayPreLoadID);
			var binaryLoader:* = new URLLoader();
			binaryLoader.dataFormat = "binary";
			binaryLoader.addEventListener(ProgressEvent.PROGRESS, this.binaryLoading);
			binaryLoader.addEventListener(Event.COMPLETE, this.binaryLoadComplete);
			binaryLoader.addEventListener(IOErrorEvent.IO_ERROR, this.binaryLoadError);
			var tmpURL:* = this._docURL + "pn=" + (this._pagesLoaded + 1) + "&rn=" + this._normalPageNum;
			var binaryRequest:* = new URLRequest(tmpURL);
			try
			{
				binaryLoader.load(binaryRequest);
				this._inLoading = true;
			}// end try
			catch (err)
			{
				_inLoading = false;
				trace("load binary data error in:" + _docURL);
			}// end catch
			return;
		}// end function
		
		private function onConvertFinished(param1:Event = null) : void
		{
			var chld:Sprite;
			var chldx:Number;
			var chldy:Number;
			var xfp:int;
			var xtp:int;
			var x:int;
			var event:* = param1;
			var evt:* = event;
			trace("onConvertFinished...");
			this._loadPercent = 0;
			trace("OK");
			var p:* = this._convertLoader.content as Sprite;
			var wscale:* = this.DV_PAGE_WIDTH / p.width;
			p.width = this.DV_PAGE_WIDTH;
			p.height = p.height * wscale;
			var pc:* = new Sprite();
			var pcgp:* = pc.graphics;
			pcgp.lineStyle(0, 0);
			pcgp.drawRect(0, 0, p.width / wscale, p.height / wscale);
			p.addChild(pc);
			if (this._showLoading)
			{
				p.x = 0;
				if (this._containerHeight)
				{
					p.y = this._containerHeight + this.Y_PADDING;
				}
				else
				{
					p.y = 0;
				}// end else if
				this._container.addChild(p);
			}
			else
			{
				chld = this._container.getChildAt(this._pagethLoading) as Sprite;
				chldx = chld.x;
				chldy = chld.y;
				this._container.removeChild(chld);
				this._container.addChildAt(p, this._pagethLoading);
				p.x = chldx;
				p.y = chldy;
			}// end else if
			try
			{
				this._scrollPane.update();
			}// end try
			catch (err)
			{
				trace("no _scrollPane no updata");
			}// end catch
			var _loc_3:Object;
			  this._hasConvertPages++;
		 
			 this._pagethLoading++;
			if (!this._pageWidth)
			{
				this._pageWidth = p.width;
			}
			else
			{
				this._pageWidth = Math.max(this._pageWidth, p.width);
			}// end else if
			if (!this._pageHeight)
			{
				this._pageHeight = p.height;
			}
			else
			{
				this._pageHeight = Math.max(this._pageHeight, p.height);
			}// end else if
			if (this._hasConvertPages == this._allConvertPages)
			{
				if (!this._pagesLoadedArray)
				{
					this._pagesLoadedArray = new Array(this._pagesAll);
				}
				xfp = this._fromPage;
				xtp = this._toPage;
				x = (xfp - 1);
				while (x < xtp)
				{
					
					this._pagesLoadedArray[x] = 1;
					x = (x + 1);
				}
				if (!this._pageth)
				{
					this._pageth = 0;
				}
				if (this._showLoading)
				{
					this.addBlankPages();
				}
			}
			else
			{
				this._containerWidth = this._container.width;
				this._containerHeight = this._container.height;
				this.byteArr2DisplayObj(this._hasConvertPages);
			}// end else if
			return;
		}// end function
		
		private function add2ScrollPane() : void
		{
			clearInterval(this._convertErrorIntervalID);
			if (!this._scrollPane.content)
			{
				this._scrollPane.setSize(this.DV_WIDTH-1, this.DV_HEIGHT - 9);
				this._scrollPane.content = this._container;
				this._scrollPane.content.x = Math.max((this.DV_WIDTH - this._container.width) / 2, 0);
				this._scrollPane.hScrollPosition = this._scrollPane.maxHScrollPosition / 2;
			}// end if
			this._scrollPane.update();
			this._container.addEventListener(MouseEvent.ROLL_OVER, this.docMouseOver);
			this._container.addEventListener(MouseEvent.ROLL_OUT, this.docMouseOut);
			this._container.addEventListener(MouseEvent.MOUSE_DOWN, this.docMouseDown);
			return;
		}// end function
		
		private function docScroll_1016_scrollBar_page_error(param1:ScrollEvent = null) : void
		{
			var _loc_2:Sprite;
			var _loc_3:* = this._pageth;
			var _loc_4:* = param1 ? (param1.delta) : (0);
			if ((param1 ? (param1.delta) : (0)) > 0)
			{
				if (_loc_3 < this._pagesAll--)
				{
					_loc_2 = this._container.getChildAt(_loc_3 + 1) as Sprite;
					if (this._container.localToGlobal(new Point(_loc_2.x, _loc_2.y)).y < stage.stageHeight / 2)
					{
					}// end if
				}// end if
			}
			else if (_loc_4 < 0)
			{
				if (_loc_3++ > 0)
				{
					_loc_2 = this._container.getChildAt(_loc_3--) as Sprite;
					if (this._container.localToGlobal(new Point(_loc_2.x, _loc_2.y)).y + _loc_2.height * this.zoom > stage.stageHeight / 2)
					{
					}// end if
				}// end if
			}// end else if
			if (this._pagesLoaded < this._pagesAll)
			{
				if (this._delayPreLoadID)
				{
					clearInterval(this._delayPreLoadID);
				}// end if
				this._delayPreLoadID = setInterval(this.preLoad, 1000, _loc_4);
			}// end if
			if (this._pageth != _loc_3--)
			{
				this._pageth = _loc_3;
				dispatchEvent(new Event("PAGETH_CHANGED", true));
			}// end if
			return;
		}// end function
		
		public function get loadPercent() : Number
		{
			return this._loadPercent;
		}// end function
		
		private function processNoDoc() : void
		{
			this._pagesAll = 0;
			this._pagesLoaded = 0;
			this._pageth = 0;
			if (!this._noDoc)
			{
				this._noDoc = new TextField();
				this._noDoc.text = "文档载入异常，您可以下载后查看";
				this._noDoc.autoSize = "left";
				this._noDoc.selectable = false;
				stage.addChild(this._noDoc);
			}// end if
			this._noDoc.x = (stage.stageWidth - this._noDoc.width) / 2;
			this._noDoc.y = (stage.stageHeight - this._noDoc.height) / 2;
			dispatchEvent(new Event("PAGE_LOADED", true));
			return;
		}// end function
		
		private function doStageMouseWheelInFull_1026_full_same_normal(param1:MouseEvent = null) : void
		{
			if (this._displayState == "normal")
			{
				return;
			}// end if
			var _loc_2:* = this._pageth;
			var _loc_3:* = param1.delta < 0 ? (1) : (-1);
			if (this._displayState == "fullSingle")
			{
				_loc_2 = _loc_2 + 1 * _loc_3;
			}
			else if (this._displayState == "fullDouble")
			{
				_loc_2 = _loc_2 + 2 * _loc_3;
			}// end else if
			if (_loc_2 != this._pageth)
			{
				this.pageth = Math.max(0, Math.min(_loc_2, this._pagesAll--));
				dispatchEvent(new Event("PAGETH_CHANGED", true));
			}// end if
			return;
		}// end function
		
		public function set pageth(param1:uint) : void
		{
			var _loc_2:Sprite;
			if (param1 != this._pageth)
			{
				this._pageth = param1;
				_loc_2 = this._container.getChildAt(this._pageth) as Sprite;
				this._scrollPane.vScrollPosition = _loc_2.y * this.zoom;
			}// end if
			return;
		}// end function
		
		private function onConverting(param1:ProgressEvent = null) : void
		{
			this._loadPercent = this._hasConvertPages / this._pagesAll;
			if (this._showLoading)
			{
				dispatchEvent(new Event("SHOW_LOADING", true));
			}// end if
			trace("converting:" + this._loadPercent);
			return;
		}// end function
		
		private function docMouseDown(param1:MouseEvent = null) : void
		{
			this._positionX = stage.mouseX;
			this._positionY = stage.mouseY;
			stage.addEventListener(MouseEvent.MOUSE_MOVE, this.docMouseMove);
			stage.addEventListener(MouseEvent.MOUSE_UP, this.docMouseUp);
			var _loc_2:* = CursorManager.getInstance();
			if (!_loc_2.root)
			{
				_loc_2.root = stage;
			}// end if
			if (!this._inSwitching)
			{
				_loc_2.showCursor("DragCursor");
			}// end if
			return;
		}// end function
		
		private function stageMouseEvent(param1:MouseEvent) : void
		{
			this._mouseDown = param1.buttonDown;
			return;
		}// end function
		
		private function onConvertError(param1:Event = null) : void
		{
			var chld:Sprite;
			var chldx:Number;
			var chldy:Number;
			var xfp:int;
			var xtp:int;
			var x:int;
			var event:* = param1;
			var evt:* = event;
			trace("onConvertError start: " + evt);
			this._loadPercent = 0;
			var p:* = new BlankPage();
			p.pageth = this._fromPage + this._hasConvertPages;
			p.setSize(this.DV_PAGE_WIDTH, this._pageHeight ? (this._pageHeight) : (200));
			p.error = true;
			this._container.addChild(p);
			if (this._showLoading)
			{
				p.x = 0;
				if (this._containerHeight)
				{
					p.y = this._containerHeight + this.Y_PADDING;
				}
				else
				{
					p.y = 0;
				}// end else if
				this._container.addChild(p);
			}
			else
			{
				chld = this._container.getChildAt(this._pagethLoading) as Sprite;
				chldx = chld.x;
				chldy = chld.y;
				this._container.removeChild(chld);
				this._container.addChildAt(p, this._pagethLoading);
				p.x = chldx;
				p.y = chldy;
			}// end else if
			try
			{
				this._scrollPane.update();
			}// end try
			catch (err)
			{
				trace("no _scrollPane no updata");
			}// end catch
			var _loc_3:Object;
			_loc_3._hasConvertPages = this._hasConvertPages++;
		 
			_loc_3._pagethLoading = this._pagethLoading++;
			if (!this._pageWidth)
			{
				this._pageWidth = p.width;
			}
			else
			{
				this._pageWidth = Math.max(this._pageWidth, p.width);
			}// end else if
			if (!this._pageHeight)
			{
				this._pageHeight = p.height;
			}
			else
			{
				this._pageHeight = Math.max(this._pageHeight, p.height);
			}// end else if
			if (this._hasConvertPages == this._allConvertPages)
			{
				if (!this._pagesLoadedArray)
				{
					this._pagesLoadedArray = new Array(this._pagesAll);
				}// end if
				xfp = this._fromPage;
				xtp = this._toPage;
				x = xfp--;
				while (x < xtp)
				{
					// label
					this._pagesLoadedArray[x] = 1;
					x = x++;
					x = x;
				}// end while
				if (!this._pageth)
				{
					this._pageth = 0;
				}// end if
				if (this._showLoading)
				{
					this.addBlankPages();
				}// end if
			}
			else
			{
				this._containerWidth = this._container.width;
				this._containerHeight = this._container.height;
				this.byteArr2DisplayObj(this._hasConvertPages);
			}// end else if
			return;
		}// end function
		
		private function doMouseWheel(param1:Number) : void
		{
			 
		}// end function
		
		public function doSearchX(param1:int, param2:String) : void
		{
			var _loc_3:Sprite;
			var _loc_4:int;
			var _loc_5:int;
			var _loc_6:String;
			var _loc_7:int;
			if (!param2 || param2 == "")
			{
				return;
			}// end if
			var _loc_8:Array;
			_loc_8 = param2.split(" ");
			var _loc_9:Number;
			this._searchPageth = param1;
			var _loc_10:int;
			while (_loc_10 < 3)
			{
				// label
				if (_loc_10 + param1 > this._pagesAll--)
				{
					break;
				}// end if
				_loc_3 = this._container.getChildAt(_loc_10 + param1) as Sprite;
				if (_loc_10 == 0)
				{
					this._scrollPane.vScrollPosition = _loc_3.y * this.zoom;
				}// end if
				this["_snapText" + _loc_10] = _loc_3.textSnapshot;
				this["_snapText" + _loc_10].setSelected(0, this["_snapText" + _loc_10].charCount, false);
				_loc_4 = 0;
				_loc_5 = _loc_8.length;
				while (_loc_4 < _loc_5)
				{
					// label
					_loc_6 = _loc_8[_loc_4];
					if (_loc_6 != "")
					{
						_loc_7 = this["_snapText" + _loc_10].findText(0, _loc_6, false);
						if (_loc_7 == -1)
						{
						}// end if
						if (_loc_7 > 0)
						{
							do
							{
								// label
								trace(_loc_7);
								this["_snapText" + _loc_10].setSelectColor(this.HIGHLIGHT_COLOR);
								this["_snapText" + _loc_10].setSelected(_loc_7, _loc_7 + _loc_6.length, true);
								_loc_7 = this["_snapText" + _loc_10].findText(_loc_7 + _loc_6.length, _loc_6, false);
							}while (_loc_7 > 0)
						}// end if
					}
					else
					{
						this["_snapText" + _loc_10].setSelected(0, this["_snapText" + _loc_10].charCount, false);
					}// end else if
					_loc_4++;
				}// end while
				_loc_10++;
			}// end while
			this._resultNum = _loc_9++;
			dispatchEvent(new Event("TO_SEARCH_RESULT", true));
			return;
		}// end function
		
		private function docMouseDownX(param1:MouseEvent = null) : void
		{
			var _loc_2:String;
			if (this._displayState == "normal")
			{
				this._positionX = stage.mouseX;
				this._positionY = stage.mouseY;
				stage.addEventListener(MouseEvent.MOUSE_MOVE, this.docMouseMove);
				_loc_2 = "DragCursor";
			}// end if
			var _loc_3:* = CursorManager.getInstance();
			if (!_loc_3.root)
			{
				_loc_3.root = stage;
			}// end if
			_loc_3.showCursor(_loc_2);
			stage.addEventListener(MouseEvent.MOUSE_UP, this.docMouseUp);
			return;
		}// end function
		
		public function set zoom(param1:Number) : void
		{
			if (param1 != this._curZoom)
			{
				this._curZoom = param1;
				this.changeZoom(this._curZoom);
			}// end if
			return;
		}// end function
		
		public function switchPagesMode(param1:String) : void
		{
			var _loc_2:Sprite;
			if (this._displayState == "normal")
			{
				return;
			}// end if
			var _loc_3:* = param1 == "fullDouble" ? (1) : (0);
			this._displayState = param1;
			var _loc_4:* = this._container.numChildren;
			var _loc_5:*;
			while (_loc_5++ < _loc_4)
			{
				// label
				_loc_2 = this._container.getChildAt(_loc_5) as Sprite;
				_loc_2.x = _loc_3 * (_loc_5 % 2) * (_loc_2.width + this.X_PADDING);
				_loc_2.y = Math.floor(_loc_5 / (_loc_3 + 1)) * (_loc_2.height + this.Y_PADDING);
			}// end while
			this._scrollPane.update();
			this._scrollPane.setSize(stage.stageWidth--, stage.stageHeight - 43);
			this._scrollPane.content.x = Math.max((stage.stageWidth - this._container.width) / 2, 0);
			this._scrollPane.hScrollPosition = this._scrollPane.maxHScrollPosition / 2;
			this._scrollPane.update();
			_loc_2 = this._container.getChildAt(this._pageth) as Sprite;
			this._scrollPane.vScrollPosition = _loc_2.y * this.zoom;
			this.docScroll();
			return;
		}// end function
		
		public function changeZoom(param1:Number) : void
		{
			this._container.scaleX = param1;
			this._container.scaleY = param1;
			this._scrollPane.content.x = Math.max((stage.stageWidth - this._container.width) / 2, 0);
			this._scrollPane.hScrollPosition = this._scrollPane.maxHScrollPosition / 2;
			this._scrollPane.update();
			var _loc_2:* = this._container.getChildAt(this._pageth) as Sprite;
			this._scrollPane.vScrollPosition = _loc_2.y * param1;
			return;
		}// end function
		
		private function docMouseMove(param1:MouseEvent = null) : void
		{
			var _loc_2:* = stage.mouseX - this._positionX;
			var _loc_3:* = stage.mouseY - this._positionY;
			this._positionX = stage.mouseX;
			this._positionY = stage.mouseY;
			this._scrollPane.hScrollPosition = this._scrollPane.hScrollPosition - _loc_2;
			this._scrollPane.vScrollPosition = this._scrollPane.vScrollPosition - _loc_3;
			return;
		}// end function
		
		private function binaryLoadError(param1:IOErrorEvent = null) : void
		{
			this._loadPercent = 0;
			dispatchEvent(new Event("STOP_LOADING", true));
			this._inLoading = false;
			trace(param1);
			this.processNoDoc();
			return;
		}// end function
		
		private function gotoPage(param1:uint) : void
		{
			this.pageth = (param1 - 1) || 0;
			return;
		}// end function
		
		private function byteArr2DisplayObj(param1:uint) : void
		{
			var param1:* = param1;
			var i:* = param1;
			this._convertLoader = new Loader();
			this._convertLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, this.onConverting);
			this._convertLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, this.onConvertFinished);
			this._convertLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, this.onConvertError);
			this._convertLoader.contentLoaderInfo.addEventListener(ErrorEvent.ERROR, this.onConvertError);
			try
			{
				trace(i + "==========================" + this._byteArray[i].bytesAvailable);
				this._convertLoader.loadBytes(this._byteArray[i] as ByteArray);
				trace(this._convertLoader.content);
			}// end try
			catch (e)
			{
				trace("byteArr2DisplayObj convert error");
			}// end catch
			return;
		}// end function
		
		private function docMouseOver(param1:MouseEvent = null) : void
		{
			var _loc_2:* = CursorManager.getInstance();
			if (!_loc_2.root)
			{
				_loc_2.root = stage;
			}// end if
			if (!param1.buttonDown && !this._inSwitching)
			{
				_loc_2.showCursor("ReadCursor");
			}// end if
			return;
		}// end function
		
		public function get zoom() : Number
		{
			return this._curZoom;
		}// end function
		
		public function set displayState(param1:String) : void
		{
			if (param1 != this._displayState)
			{
				this._displayState = param1;
			}// end if
			return;
		}// end function
		
		private function binaryLoadComplete(param1:Event = null) : void
		{
			var _loc_2:ByteArray;
			var _loc_3:int;
			trace("binaryLoadComplete...");
			if (this._noDoc)
			{
				if (this._noDoc.parent)
				{
					this._noDoc.parent.removeChild(this._noDoc);
				}// end if
				this._noDoc = null;
			}// end if
			this._loadPercent = 0;
		 
			var _loc_5:* = URLLoader(param1.target).data;
			this._byteArray = [];
			var _loc_6:Array = [];
			var _loc_7:int = 0;
			var _loc_8:* = _loc_5.length;//返回数据大小
			while (_loc_7 < _loc_8)
			{
				
				if (_loc_7 + 3 < _loc_8)
				{
					if (_loc_5[_loc_7] == 67 && _loc_5[(_loc_7 + 1)] == 87 && _loc_5[_loc_7 + 2] == 83 && (_loc_5[_loc_7 + 3] == 6 || _loc_5[_loc_7 + 3] == 9 || _loc_5[_loc_7 + 3] == 10))
					{
						trace("version:" + _loc_5[_loc_7 + 3]);
						_loc_6.push(_loc_7);
					}
				}
				else
				{
					_loc_6.push(_loc_8);
					break;
				}
				_loc_7++;
			}
			trace(_loc_6.length);
			var _loc_9:* = _loc_5.readMultiByte(_loc_6[0], "utf-8");
			trace(_loc_9);
			var _loc_10:* = JSON.decode(_loc_9);
			this._pagesAll = Number(_loc_10["totalPage"]);
			if (!this._pagesLoaded)
			{
				this._pagesLoaded = 0;
			}// end if
			this._pagesLoaded = this._pagesLoaded + (Number(_loc_10["toPage"]) - Number(_loc_10["fromPage"]) + 1);
			this._fromPage = Number(_loc_10["fromPage"]);
			this._toPage = Number(_loc_10["toPage"]);
			trace("pagesall:" + this._pagesAll + " , frompage:" + this._fromPage + " , topage:" + this._toPage);
			this._pagethLoading = Number(_loc_10["fromPage"])-1;
			var _loc_11:int;
			while (_loc_11 < _loc_6.length--)
			{
				// label
				_loc_2 = new ByteArray();
				var pagelength:int = _loc_6[_loc_11 + 1] - _loc_6[_loc_11];
				_loc_5.readBytes(_loc_2, 0, pagelength);
				this._byteArray.push(_loc_2);
				_loc_11++;
			}// end while
			trace("...............这一次加载了多少页：" + this._byteArray.length);
			this._hasConvertPages = 0;
			this._allConvertPages = Math.min(this._byteArray.length, this._toPage - this._fromPage + 1);
			if (this._allConvertPages > 0)
			{
				this.byteArr2DisplayObj(this._hasConvertPages);
			}
			else
			{
				trace("blank document ...");
				this.processNoDoc();
				this._loadPercent = 0;
				dispatchEvent(new Event("STOP_LOADING", true));
				this._inLoading = false;
			}// end else if
			return;
		}// end function
		
		private function docScrollX(param1:ScrollEvent = null) : void
		{
			var _loc_2:Sprite;
			var _loc_3:* = this._pageth;
			if (param1.delta > 0)
			{
				if (_loc_3 < this._pagesLoaded--)
				{
					_loc_2 = this._container.getChildAt(_loc_3 + 1) as Sprite;
					if (this._container.localToGlobal(new Point(_loc_2.x, _loc_2.y)).y < stage.stageHeight / 2)
					{
					}// end if
				}// end if
			}
			else if (_loc_3++ > 0)
			{
				_loc_2 = this._container.getChildAt(_loc_3--) as Sprite;
				if (this._container.localToGlobal(new Point(_loc_2.x, _loc_2.y)).y + _loc_2.height > stage.stageHeight / 2)
				{
				}// end if
			}// end else if
			if (param1.delta > 0 && !this._inLoading && this._pagesLoaded < this._pagesAll && _loc_3-- >= this._pagesLoaded--)
			{
				if (this._delayPreLoadID)
				{
					clearInterval(this._delayPreLoadID);
				}// end if
			}// end if
			trace("new page:" + _loc_3);
			if (this._pageth != _loc_3)
			{
				this._pageth = _loc_3;
				dispatchEvent(new Event("PAGETH_CHANGED", true));
			}// end if
			return;
		}// end function
		
		private function docMouseOut(param1:MouseEvent = null) : void
		{
			if (!param1.buttonDown && !this._inSwitching)
			{
				CursorManager.getInstance().hideCurrentCursor();
			}// end if
			return;
		}// end function
		
		private function docScrollXX(param1:ScrollEvent = null) : void
		{
			var _loc_2:* = this._scrollPane.vScrollPosition / this._scrollPane.maxVScrollPosition;
			var _loc_3:* = Math.floor(_loc_2 * this._pagesLoaded);
			trace(param1.delta, this._inLoading, this._pagesLoaded, this._pagesAll, _loc_3);
			if (param1.delta > 0 && !this._inLoading && this._pagesLoaded < this._pagesAll && _loc_3 >= this._pagesLoaded--)
			{
				if (this._delayPreLoadID)
				{
					clearInterval(this._delayPreLoadID);
				}// end if
				this._delayPreLoadID = setInterval(this.preLoad, 1000);
			}// end if
			if (_loc_3 == this._pagesLoaded)
			{
			}// end if
			if (this._pageth != _loc_3--)
			{
				this._pageth = _loc_3;
				dispatchEvent(new Event("PAGETH_CHANGED", true));
			}// end if
			return;
		}// end function
		
		public function get pagesLoaded() : uint
		{
			return this._pagesLoaded;
		}// end function
		
		public function get fpn() : int
		{
			return this._firstPagesNum;
		}// end function
		
		private function docMouseUp(param1:MouseEvent = null) : void
		{
			if (this._container.hitTestPoint(mouseX, mouseY) && !this._inSwitching)
			{
				CursorManager.getInstance().showCursor("ReadCursor");
			}
			else
			{
				CursorManager.getInstance().hideCurrentCursor();
			}// end else if
			this._positionX = 0;
			this._positionY = 0;
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.docMouseMove);
			stage.removeEventListener(MouseEvent.MOUSE_UP, this.docMouseUp);
			return;
		}// end function
		
		public function set npn(param1:int) : void
		{
			this._normalPageNum = param1;
			return;
		}// end function
		
		private function binaryLoading(param1:ProgressEvent = null) : void
		{
			this._loadPercent = param1.bytesLoaded / param1.bytesTotal;
			if (this._showLoading)
			{
				dispatchEvent(new Event("SHOW_LOADING", true));
			}// end if
			return;
		}// end function
		
		private function doStageMouseWheelInFull(param1:MouseEvent = null) : void
		{
			trace(param1.delta);
			if (this._displayState == "normal")
			{
				return;
			}// end if
			if (!(-param1.delta))
			{
				this.doMouseWheel(0);
			}// end if
			return;
		}// end function
		
		public function get displayState() : String
		{
			return this._displayState;
		}// end function
		
	}
}
